
local meta = FindMetaTable( "Player" )

if not meta then return end 

AIDS = {}

if SERVER then

	AddCSLuaFile( "aids/aids.lua" )
	
end

function AIDS.ChatPrint( tbl )
	
	local str = ""
	
	for k,v in pairs( tbl ) do
		
		if type( v ) == "Player" then
			
			local col = team.GetColor( v:Team() )
			str = str .. ", Color(" .. col.r .."," .. col.g .. "," .. col.b .. "), \"" .. v:Nick() .. "\", Color( 255, 255, 255 )"
			
		else
			
			str = str .. ", \"" .. v .. "\""
			
		end
		
	end

	BroadcastLua( "chat.AddText( Color( 0, 255, 150 ), \"[AIDS] \", Color( 255, 255, 255 ), \" \" " .. str .. ")" )

end

function AIDS.AddCommand( func, name )
	
	concommand.Add( name, function( ply, cmd, args ) if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) then return end func( ply, cmg, args ) end )
	
end

function AIDS.Hook( name, cmdname, func )
	
	hook.Add( name, name .. "_" .. cmdname, func )

end

function AIDS.GetPlayer( id )

	local ply = Entity( id )
	
	if not ValidEntity( ply ) or not ply:IsPlayer() then return end
	
	return ply

end

AIDS.Titles = { "You've Got AIDS!", 
"Administrative Integrated Discipline System", 
"This Server Brought To You By: AIDS",
"This Looks Like A Job For AIDS!",
"AIDS? In My Server?!",
"Always Bet On AIDS",
"If You Enjoy Having AIDS Then Recommend It To A Friend!",
"No Server Is Secure Without AIDS",
"Have AIDS, Will Travel",
"The Cure For Server Cancer? AIDS!",
"All Work And No AIDS Makes Jack A Dull Boy" }

AIDS.Commands = {}
AIDS.Tabs = {}

function AIDS.RegisterCommand( name, commandname, chatcmd, args )

	table.insert( AIDS.Commands, { Name = name, CommandName = commandname, ChatCmd = chatcmd, Args = args } )

end

function AIDS.AddMenuTab( name, icon, desc, panelfunc, func )

	table.insert( AIDS.Tabs, { Name = name, PanelFunc = panelfunc, Icon = icon, Desc = desc, Func = func } )

end

function AIDS.Menu( ply, cmd, args )

	if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) then return end
	
	local xsize = 600
	local ysize = 500
	local players = {}

	local panel = vgui.Create( "DFrame" )
	panel:SetTitle( table.Random( AIDS.Titles ) )
	panel:SetSize( xsize, ysize )
	panel:SetSizable( true )
	panel:Center()
	panel:MakePopup()
	
	local list = vgui.Create( "DPropertySheet", panel )
	list:StretchToParent( 4, 24, 4, 4 )
	
	local listview = vgui.Create( "DListView" )
	local col1 = listview:AddColumn( "Steam ID" )
	local col2 = listview:AddColumn( "Name" )
	
	col1:SetMinWidth( 150 )
	col1:SetMaxWidth( 150 )
	
	list:AddSheet( "Players", listview, "gui/silkicons/group", false, false, "View and manage players on the server." )
	
	for k,v in pairs( AIDS.Tabs ) do
	
		local tabpnl = v.PanelFunc()
	
		list:AddSheet( v.Name, tabpnl, v.Icon, false, false, v.Desc )
		
		if v.Func then
		
			v.Func( tabpnl )
		
		end
		
	end
	
	for k,v in pairs( player.GetAll() ) do
	
		listview:AddLine( v:SteamID(), v:Name() )
		
		table.insert( players, v )
	
	end
	
	listview.OnRowRightClick = function( line, id )
	
		local menu = vgui.Create( "DMenu", panel )
		menu:AddOption( "Cancel" )
		
		local pl = players[id]
		
		for k,v in pairs( AIDS.Commands ) do
		
			if not v.Args then
		
				menu:AddOption( v.Name, function()
			
					if ValidEntity( pl ) then
						
						if not v.Clientside then
						
							RunConsoleCommand( v.CommandName, pl:EntIndex() )
							
						end
						
						surface.PlaySound( Sound( "buttons/button14.wav" ) )
						panel:Remove()
						
					end
				
				end )
				
			else
			
				local submenu = menu:AddSubMenu( v.Name )
				
				for c,d in pairs( v.Args ) do
				
					local arg = d.Value
				
					submenu:AddOption( d.Name, function()
			
						if ValidEntity( pl ) then
							
							if type( arg ) == "table" then
							
								if not v.Clientside then
								
									local newtbl = {}
								
									for k,v in pairs( arg ) do
									
										table.insert( newtbl, tostring( v ) )
									
									end
									
									RunConsoleCommand( v.CommandName, pl:EntIndex(), unpack( newtbl ) )
									
								end
								
							else
							
								if not v.Clientside then
								
									RunConsoleCommand( v.CommandName, pl:EntIndex(), arg )
									
								end
							
							end
							
							surface.PlaySound( Sound( "buttons/button14.wav" ) )
							panel:Remove()
							
						end
				
					end )
				
				end
			
			end
			
		end
		
		menu:Open()
	
	end
	
end

if CLIENT then

	concommand.Add( "aids_menu", AIDS.Menu )
	
	function AIDS.ChatCommands( ply, text, isteam, isdead ) 
	
		local prefix = string.Left( text, 1 )
		
		if prefix == "!" and ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		
			local tbl = string.Explode( " ", string.Trim( string.Right( string.lower( text ), string.len( text ) - 1 ) ) )
			
			if #tbl < 2 then 
			
				chat.AddText( Color( 0, 255, 150 ), "[AIDS] ", Color( 255, 255, 255 ), " Be more specific, dipshit!" )
				
				return
				
			end
			
			for k,v in pairs( AIDS.Commands ) do
			
				if string.find( string.lower( v.ChatCmd ), tbl[1] ) then
				
					for c,d in pairs( player.GetAll() ) do
					
						if string.find( string.lower( d:Nick() ), tbl[2] ) or string.Left( tbl[2], 1 ) == '*' then
						
							if v.Args then
							
								if not tbl[3] then
								
									chat.AddText( Color( 0, 255, 150 ), "[AIDS] ", Color( 255, 255, 255 ), " You're missing parameters." )
				
									return
								
								end
							
								for f,g in pairs( v.Args ) do
								
									if type( g.Value ) == "table" then
									
										for j,k in pairs( g.Value ) do
										
											if string.find( tostring( k ), tbl[3] ) then
											
												if not v.Clientside then
								
													RunConsoleCommand( v.CommandName, d:EntIndex(), unpack( g.Value ) )
									
												end
												
												surface.PlaySound( Sound( "buttons/button14.wav" ) )
											
											end
										
										end
									
									else
									
										if string.find( tostring( g.Value ), tbl[3] ) then
										
											if not v.Clientside then
								
												RunConsoleCommand( v.CommandName, d:EntIndex(), g.Value )
									
											end
								
											surface.PlaySound( Sound( "buttons/button14.wav" ) )
										
										end
									
									end
								
								end
								
							else
						
								if not v.Clientside then
						
									RunConsoleCommand( v.CommandName, d:EntIndex() )
							
								end
								
								surface.PlaySound( Sound( "buttons/button14.wav" ) )
							
							end
						
						end
					
					end
				
				end
			
			end
		
		end
	
	end
	
	AIDS.Hook( "OnPlayerChat", "AIDS", AIDS.ChatCommands )
	
end

for k,v in pairs( file.FindInLua( "aids_modules/*.lua" ) ) do

	AddCSLuaFile( "aids_modules/" .. v )
    include( "aids_modules/" .. v )
	
end